﻿using Core.SimpleTemp.Application.MenuApp;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api.Sys
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiExplorerSettings(GroupName = "Sys")]
    [EnableCors("any")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {

        private readonly SysMenuAppService _service;

        /// <summary>
        /// 
        /// </summary>
        public MenuController(SysMenuAppService sysMenuAppService)
        {
            _service = sysMenuAppService;
        }




        #region 基础增删改查
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetList")]
        [PermissionFilter(nameof(MenuController) + "_" + nameof(MenuController.GetList),AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList()
        {
            var menus = await _service.GetListAsync();
            List<object> treeModels = new List<object>();
            foreach (var menu in menus)
            {
                treeModels.Add(new { Id = menu.Id.ToString(), Name = menu.Name, ParentId = menu.ParentId == Guid.Empty ? "00000000-0000-0000-0000-000000000000" : menu.ParentId.ToString(), menu.Type, menu.SerialNumber, menu.Url, menu.Code, menu.Remarks, });
            }
            return this.JsonSuccess(treeModels);
        }

        /// <summary>
        /// 增/改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(MenuController) + "_" + nameof(MenuController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(SysMenuDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(MenuController) + "_" + nameof(MenuController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult> DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }

        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        /// <param name="id">Guid</param>
        /// <returns></returns>
        [HttpGet("Get")]
        [PermissionFilter(nameof(MenuController) + "_" + nameof(MenuController.DeleteMutiAsync), AuthorizationConst.GET)]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            return this.JsonSuccess(model);
        }
        #endregion
    }
}